package com.leyou.gateway.config;

import com.leyou.common.auth.utils.RsaUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.security.PublicKey;

@Slf4j
@Data
@ConfigurationProperties(prefix = "ly.jwt")
public class JwtProperties implements InitializingBean {
    private String pubKeyPath;  //公钥地址
    private PublicKey publicKey;    //公钥

    //实例化配置属性
    private UserTokenProperties user = new UserTokenProperties();
    private AppTokenProperties app = new AppTokenProperties();

    @Data
    public class UserTokenProperties{
        private String cookieName;
    }

    @Data
    public class AppTokenProperties{
        private Long id;
        private String secret;
        private String headerName;
    }

    /**
     * Bean对象创建完毕后执行该方法
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            //加载公钥和私钥
            publicKey = RsaUtils.getPublicKey(pubKeyPath);
            log.info("【网关微服务】公钥加载完毕");
        } catch (Exception e){
            log.error("【网关微服务】公钥加载失败: {}",e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
